package com.member.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.member.command.WorkGroupCmd;
import com.member.controller.validation.workGroupAddUpdate;
import com.member.exception.CustomException;
import com.member.po.Page;
import com.member.po.WorkGroup;
import com.member.service.WorkGroupService;

@Controller
@RequestMapping("/workGroup")
public class WorkGroupController {
	@Autowired
	private WorkGroupService workGroupService;
	
	
	@RequestMapping("/workGroup")
	public  String workGroup(WorkGroupCmd groupCmd ,Model model,HttpServletRequest request){
		Map map=new HashMap();
		Page page = new Page();
		map.put("size", page.getSize());
		map.put("pageNum", page.getPageNum());
		
		if(groupCmd.getPageNum() != null){
			page.setPageNum(Integer.parseInt(groupCmd.getPageNum()));//更新page 当前页
			map.put("pageNum", Integer.parseInt(groupCmd.getPageNum()));
		}
		if(groupCmd.getGroupId() != null){
			map.put("pageNum", Integer.parseInt(groupCmd.getGroupId()));
		}
		List<WorkGroup> workGroupList=workGroupService.select(map);
		page.setAmontRow(workGroupService.countRows(map));
		model.addAttribute("page", page);
		model.addAttribute("workGroupList", workGroupList);
		return "/baseInfo/workGroup/workGroupList";
	}
	
	@ModelAttribute("workGroups")
	public List<WorkGroup> getClazzTypes() {		
		List<WorkGroup> workGroupList;
		try {
			workGroupList = workGroupService.select(null);
			return workGroupList;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping(value = "/editWorkGroup", method = { RequestMethod.POST,RequestMethod.GET })	
	public String editWorkGroup(Model model,@RequestParam(value = "id", required = true) Integer items_id)
			throws Exception {
		WorkGroup workGroup=workGroupService.selectById(items_id);			
		if(workGroup == null){
			throw new CustomException("修改的部门信息不存在!");
		}	
		model.addAttribute("items", workGroup);
		return "/baseInfo/workGroup/workGroupUpdate";
	}
	
	@RequestMapping("/updateWorkGroupSubmit")
	public String updateWorkGroupSubmit(Model model,	HttpServletRequest request,	Integer id,
		@Validated(value={workGroupAddUpdate.class}) WorkGroup workGroup ,BindingResult result )
	{
		if(result.hasErrors())
		{
			List<ObjectError> allErrors = result.getAllErrors();
			model.addAttribute("allErrors", allErrors);
			model.addAttribute("items", workGroup);
			return "/baseInfo/workGroup/workGroupUpdate";
		}
		try {
			workGroupService.updateById(workGroup);
			return "redirect:/workGroup/workGroup";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	
	}
	
	@RequestMapping("/addWorkGroup")	
	public String addWorkGroup()
	{	     
		return "/baseInfo/workGroup/workGroupAdd";
	}
	
	@RequestMapping(value = "/addWorkGroupSubmit")	
	public String addWorkGroupSubmit(Model model,
			@Validated (value={workGroupAddUpdate.class}) WorkGroup workGroup,BindingResult result)
	{	
		if(result.hasErrors())
		{
			
			List<ObjectError> allErrors = result.getAllErrors();
			model.addAttribute("allErrors", allErrors);
		    model.addAttribute("items", workGroup);
		    return "/baseInfo/workGroup/workGroupAdd";
		}
		
		try {
			workGroupService.insert(workGroup);
			return "redirect:/workGroup/workGroup";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}
	
	@RequestMapping(value = "/deleteWorkGroup")
	public String deleteWorkGroup(Model model,@RequestParam(value = "id", required = true) Integer items_id)
	{
		try {
			workGroupService.deleteById(items_id);
			return "redirect:/workGroup/workGroup";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}
	
	@RequestMapping("/deleteWorkGroups")
	public String deleteWorkGroups(Model model, Integer[] id){
		if(id != null){
			for(Integer i:id){
				workGroupService.deleteById(i);
			}
			return "redirect:/workGroup/workGroup";
		}
		return "error";
	}
	
	

}
